package com.campus.controller.user;

import com.campus.dto.ChatRecordsDTO;
import com.campus.result.Result;
import com.campus.service.AIService;
import com.campus.vo.ChatRecordsVO;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 调用AI相关的 Controller
 *
 * @author TaiZai
 * @data 2024/11/9 15:58
 */
@RestController
@RequestMapping("/ai")
@Slf4j
public class AIController {

    @Autowired
    private AIService aiService;

    /**
     * 测试高亮的一个 markdown 文本
     *
     * @return
     */
    @GetMapping("/test")
    public Result<String> testAI() {
        String str = "要设计一个类似于ChatGPT的回复样式，您可以通过以下步骤来实现，确保它包含Markdown样式支持和代码高亮功能。下面是实现的主要思路：\n" +
                "\n" +
                "### 1. 支持Markdown格式\n" +
                "Markdown支持使得文本能够格式化，例如**加粗**、*斜体*、[链接](#)、标题、列表等。可以选择使用支持Markdown解析的库，例如在Python中使用 `markdown` 或 `mistune`，在前端使用 `marked.js` 等。\n" +
                "\n" +
                "### 2. 支持代码高亮\n" +
                "为了实现代码高亮，可以结合Markdown解析器和代码高亮工具，例如`highlight.js`。这种组合可以实现代码块的语法高亮，从而方便用户阅读。\n" +
                "\n" +
                "### 3. 代码实现\n" +
                "这里是一个简单的HTML和JavaScript实现，使用`marked.js`和`highlight.js`来展示具有Markdown格式和代码高亮的文本：\n" +
                "\n" +
                "```html\n" +
                "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>ChatGPT-style Markdown and Code Highlight</title>\n" +
                "    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/default.min.css\">\n" +
                "    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.12/marked.min.js\"></script>\n" +
                "    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js\"></script>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div id=\"output\" style=\"border: 1px solid #ddd; padding: 20px;\"></div>\n" +
                "\n" +
                "    <script>\n" +
                "        // 示例Markdown内容，包含代码块\n" +
                "        const markdownContent = `\n" +
                "# ChatGPT-style Markdown and Code Highlight\n" +
                "\n" +
                "This is an example of **Markdown** with a code block:\n" +
                "\n" +
                "\\`\\`\\`python\n" +
                "def hello_world():\n" +
                "    print(\"Hello, world!\")\n" +
                "\\`\\`\\`\n" +
                "\n" +
                "- **Bold Text**\n" +
                "- *Italic Text*\n" +
                "- [Link](#)\n" +
                "        `;\n" +
                "\n" +
                "        // 配置marked.js，启用代码高亮\n" +
                "        marked.setOptions({\n" +
                "            highlight: function(code, lang) {\n" +
                "                const language = hljs.getLanguage(lang) ? lang : 'plaintext';\n" +
                "                return hljs.highlight(code, { language }).value;\n" +
                "            }\n" +
                "        });\n" +
                "\n" +
                "        // 将Markdown渲染为HTML并插入页面\n" +
                "        document.getElementById('output').innerHTML = marked(markdownContent);\n" +
                "\n" +
                "        // 初始化highlight.js\n" +
                "        hljs.highlightAll();\n" +
                "    </script>\n" +
                "</body>\n" +
                "</html>\n" +
                "```\n" +
                "\n" +
                "### 4. 解释代码\n" +
                "- **marked.js**：这是一个Markdown解析库，用于将Markdown内容转换成HTML。\n" +
                "- **highlight.js**：这是一个代码高亮库，用于在转换后的HTML中对代码块进行语法高亮。\n" +
                "- **marked.setOptions()**：配置`marked`，让它在解析Markdown时对代码块进行高亮处理。\n" +
                "- **`highlightAll()`**：确保页面上所有的代码块都被高亮。\n" +
                "\n" +
                "### 5. 使用步骤\n" +
                "将上述代码保存为`.html`文件，然后打开它。页面会显示一个包含Markdown样式和代码高亮的文本框，您可以将Markdown内容放入`markdownContent`变量中。\n" +
                "\n" +
                "这样，就可以实现一个类似ChatGPT的回复样式，包含Markdown样式支持和代码高亮功能。";
        return Result.success(str);
    }

    /**
     * 根据传入的聊天记录生成标题再插入数据库并将记录返回
     *
     * @param chatRecordsDTO
     * @param userId
     * @return
     */
    @PostMapping("/title/{id}")
    public Result<ChatRecordsVO.item> makeChatTitle(@RequestBody ChatRecordsDTO chatRecordsDTO, @PathVariable("id") Long userId) {
        // log.info("AI生成聊天记录：{}，userId：{}", chatRecordsDTO, userId);
        ChatRecordsVO.item record = aiService.makeChatTitle(chatRecordsDTO.getContent(), userId);
        return Result.success(record);
    }

    /**
     * 获取某个用户的全部聊天记录
     *
     * @param userId
     * @return
     */
    @GetMapping("/records/{id}")
    public Result<List<ChatRecordsVO>> queryChatRecordsByUserId(@PathVariable("id") Long userId) {
        List<ChatRecordsVO> records = aiService.queryChatRecordsByUserId(userId);
        return Result.success(records);
    }

    /**
     * 根据ID获取聊天记录数据
     *
     * @param recordId
     * @return
     */
    @GetMapping("/record/{id}")
    public Result<ChatRecordsVO.item> queryChatRecordById(@PathVariable("id") Long recordId) {
        ChatRecordsVO.item record = aiService.queryChatRecordById(recordId);
        return Result.success(record);
    }

    /**
     * 更新聊天记录数据
     *
     * @param record
     * @return
     */
    @PostMapping("/update")
    public Result<String> updateChatRecord(@RequestBody ChatRecordsVO.item record) {
        aiService.updateChatRecord(record);
        return Result.success();
    }

    /**
     * 删除单个聊天记录
     * @param recordId
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteChatRecord(@PathVariable("id") Long recordId) {
        aiService.deleteChatRecord(recordId);
        return Result.success();
    }
}
